<?php
namespace Admin\Controller;
use Think\Controller;
/*
会员管理
 */
class UserController extends CommonController {
	//会员列表--------------------------------------------------------------------------------
    public function userlist()
    {	
    	$data=I('post.');
    	$stime=strtotime($data['starttime']);
    	$etime=strtotime($data['endtime']);
    	$name=$data['nickname'];

        //搜索
        $where="status='0'";
        if ($stime) {
            $where.=" and create_time>'$stime'";
        }
        if ($etime) {
            $where.=" and create_time>'$etime'";
        }
        if ($name) {
            $where.=" and create_time>'$name'";
        }
    	
    	$sreach=M('user')->where($where)->select();
        // var_dump($sreach);
    	if ($sreach) {

    	$this->assign('userlist',$sreach);
    	
    	}else{

    	$count      = M('user')->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

		$sql="select * from user limit ".$Page->firstRow.','.$Page->listRows;
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$userlist=M('user')->query($sql);

		$this->assign('page',$show);// 赋值分页输出

    	$this->assign('userlist',$userlist);
    	}

    	$this->display();
    }
    //会员列表点人名弹出的方法	
    public function usershow()
    {
    	$id=I('get.id');
    	$show=M('user')->where("id='$id'")->find();
    	$this->assign('show',$show);
    	$this->display();
    }
    //会员列表停用--------------------------------------------------------------------------------
    public function userstop()
    {
    	$uid=I('post.uid');
    	// var_dump($uid);
    	$data['status']=1;
    	$data['id']=$uid;
    	if (M('user')->save($data)) {
    		Reajax(0,"停用成功");
    	}else{
    		Reajax(1,"停用失败");
    	}
    }
    //会员列表启用--------------------------------------------------------------------------------
    public function userstart()
    {
    	$uid=I('post.uid');
    	$data['status']=0;
    	$data['id']=$uid;
    	if (M('user')->save($data)) {
    		Reajax(0,"启用成功");
    	}else{
    		Reajax(1,"启用失败");
    	}
    }
    //会员列表编辑修改--------------------------------------------------------------------------------
    public function useredit()
    {
    	$data = M('user')->find(I('get.id'));
    	$this->assign('data',$data);
    	$this->display();
    }
    //会员列表修改用户--------------------------------------------------------------------------------
    public function add()
    {
    	$data=I('post.');
    	$id=I('post.uid');
    	// var_dump($data);
			// var_dump(M('user')->where("id='$id'")->save($data));exit;
			if (M('user')->where("id='$id'")->save($data)) {
				 
				 Reajax(0,"修改完成");

			}else{
				Reajax(1,"修改失败");
			}
    }
    //会员列表修改密码--------------------------------------------------------------------------------
    public function userpassword()
    {
    	
    	$data = M('user')->find(I('get.id'));
    	// var_dump($data);
    	$this->assign('data',$data);
    	$this->display();
    }
    //异步对比密码修改--------------------------------------------------------------------------------
    public function password()
    {	
    	$data=I('post.');
    	$uid=$data['uid'];
		if ($data['newpass']==$data['repass']) {
			$pass['password']=MD5($data['newpass']);
			M('user')->where("id='$uid'")->save($pass);
			Reajax(0,"修改密码成功");
    	}else{
    		Reajax(1,"两次密码不一致");
    	}	
    }
    //添加用户应用的模板--------------------------------------------------------------------------
    public function useradd()
    {
    	$this->display();
    }
    //后台添加用户用的方法--------------------------------------------------------------------------
    public function userreg()
    {

        // var_dump(888);exit;
    	$data=I('post.');
    	if ($data['pass']!=$data['repass']) {  		
		Reajax(1,"两次密码不一致,请重新输入");
    	}
    	D('user')->checkEmail($data['email']);
    	D('user')->checkNickname($data['username']);
    	$data['create_time']=time();
    	$data['password']=MD5($data['repass']);
    	$data['nickname']=$data['username'];
    	$arr=["/Public/Index/images/11424.gif","/Public/Index/images/26880.gif","/Public/Index/images/784224.jpg"];	
    	$data['face']=$arr[mt_rand(0,2)];
           $res= M('user')->add($data);

    	if ($res) {
    		Reajax(0,"添加成功");
    	}else{
    		Reajax(1,"添加失败");
    	}

    }
    //批量删除-------------------------------------------------------------------------
    public function userall()
    {
    	$data=I('post.uids');
    	$data=rtrim($data,',');
    	// var_dump($data);
    	// $sql="delete from user where id in (".$data.")";//这个是真的删除
    	$sql = "update user set status='1' where id in (".$data.")";
    	// var_dump($sql);exit;
    	$res=M('user')->execute($sql);
    	if ($res) {
    		Reajax(0,"批量删除成功");
    	}else{
    		Reajax(1,"批量删除失败");
    	}
    	
    }
    //会员删除-------------------------------------------------------------------------
    public function userdel()
    {
    	$data=I('get.');
    	$stime=strtotime($data['starttime']);
    	$etime=strtotime($data['endtime']);
    	$name=$data['nickname'];
    	//搜索某个人
    	$sreach=M('user')->where("nickname='$name' and create_time>='$stime' and create_time<='$etime'and status='1' ")->select();
    	if ($sreach) {
    		$this->assign('res',$sreach);
    	}else{
	    	$res=M('user')->where('status="1"')->select();
	    	$this->assign('res',$res);
    	}

    	$this->display();
    }
    //会员恢复-------------------------------------------------------------------------
    public function userreply()
    {
    	$uid=I('post.uid');
    	$data['status']=0;
    	$res=M('user')->where("id='$uid'")->save($data);
    	if ($res) {
    		Reajax(0,"恢复成功");
    	}else{
    		Reajax(1,"没有恢复");
    	}
    }
    //彻底删除无法恢复-------------------------------------------------------------------------
    public function userdelete()
    {
    	$uid=I('post.uid');
    	$res=M('user')->where("id='$uid'")->delete();
    	if ($res) {
            
            // $this->log(json_decode(array("删除用户","用户".$uid)));//记录被删除的用户id
            // $rule=M('user')->where("id='$uid'")->find();//取出这一项的数据
            // $this->log(json_decode(array("删除用户",$rule)));//记录被删除这项的数据
    		Reajax(0,"彻底删除成功");
    	}else{
    		Reajax(1,"没有彻底删除");
    	}
    }
    //批量恢复-------------------------------------------------------------------------
    public function allreply()
    {
    	$uids=I('post.uids');
    	$data=rtrim($uids,',');
    	$sql="update user set  status='0' where id in (".$data.")";
    	$res=M()->execute($sql);
    	if ($res) {
    		Reajax(0,"批量恢复成功");
    	}else{
			Reajax(1,"批量恢复失败");
    	}
    }
    //等级管理-------------------------------------------------------------------------
    public function userlevel()
    {
    	$sql="select * from level ";
    	$res=M()->query($sql);
    	// var_dump($res);
    	$this->assign('data',$res);
    	$this->display();
    }
    //批量删除会员等级-------------------------------------------------------------------------
    public function userdellevel()
    {
    	$data=I('post.uids');
    	$data=rtrim($data,',');

    	$sql="delete from level where id in (".$data.")";//这个是真的删除

    	$res=M('user')->execute($sql);
    	if ($res) {
    		Reajax(0,"批量删除成功");
    	}else{
    		Reajax(1,"批量删除失败");
    	}
    }
    //等级添加模板-------------------------------------------------------------------------
    public function userlvadd()
    {
    	// var_dump(888);
    	$this->display();
    	
    }
    //等级添加方法-------------------------------------------------------------------------
    public function userlvladd()
    {
    	$data=I('post.');
    	// var_dump($data);
    	$data['create_time']=time();
    	$data['kiss']=$data['level-kiss'];
    	$data['dengji']=$data['level-name'];
    	if (M('level')->add($data)) {
    		Reajax(0,"添加成功");
    	}else{
    		Reajax(1,"添加失败");
    	}
    }
    //等级编辑模板-------------------------------------------------------------------------
    public function userleceledit()
    {
    	$id=I('get.id');
    	$res=M('level')->where("id='$id'")->find();
    	$this->assign('data',$res);
    	$this->display();
    }
    //等级编辑方法-------------------------------------------------------------------------
    public function userleveladd()
    {
    	$data=I('post.');

    	$id=I('post.level-id');
    	$data['kiss']=$data['level-kiss'];
    	$data['dengji']=$data['level-name'];
    	$res=M('level')->where("id='$id'")->save($data);

    	if ($res) {
    		Reajax(0,"编辑成功");
    	}else{
    		Reajax(1,"编辑失败");
    	}
    }
    //等级删除-------------------------------------------------------------------------
    public function userlevel_del()
    {
    	$uid=I('post.uid');
    	$res=M('level')->where("id='$uid'")->delete();

    	if ($res) {
    		Reajax(0,"等级删除成功");
    	}else{
    		Reajax(1,"没有删除等级");
    	}
    }
    //积分管理-------------------------------------------------------------------------
    public function userkiss()
    {	
    	$sql="select * from count ";
    	$res=M()->query($sql);
    	// var_dump($res);
    	$this->assign('data',$res);

    	$this->display();
    }
    //积分异步发过来的批量删除数据
    public function userdelkiss()
    {
    	$data=I('post.uids');
    	$data=rtrim($data,',');
    	// var_dump($data);
    	$sql="delete from count where id in (".$data.") ";//真正的删除
    	$res=M()->execute($sql);
    	if ($res) {
    		Reajax(0,"批量删除成功");
    	}else{
    		Reajax(1,"批量删除失败");
    	}
    }
    //积分添加模板
    public function userkis()
    {
    	$this->display();
    }
    //积分添加方法
    public function userkisad()
    {
    	$data=I('post.');
    	$data['create_time']=time();
    	$data['kiss']=$data['level-kiss'];
    	$data['event']=$data['level-name'];
    	if (M('count')->add($data)) {
    		Reajax(0,"添加成功");
    	}else{
    		Reajax(1,"添加失败");
    	}
    }
    //积分编辑模板
    public function userpian()
    {
    	$id=I('get.id');
    	$res=M('count')->where("id='$id'")->find();
    	$this->assign('data',$res);
    	$this->display();
    }
    //积分编辑方法
    public function userbian()
    {
    	$data=I('post.');

    	$id=I('post.level-id');
    	$data['kiss']=$data['level-kiss'];
    	$data['event']=$data['level-name'];
    	$res=M('count')->where("id='$id'")->save($data);

    	if ($res) {
    		Reajax(0,"编辑成功");
    	}else{
    		Reajax(1,"编辑失败");
    	}
    }
    //积分删除
    public function userbianji()
    {
   		$uid=I('post.uid');
    	$res=M('count')->where("id='$uid'")->delete();

    	if ($res) {
    		Reajax(0,"删除成功");
    	}else{
    		Reajax(1,"没有删除");
    	}
    }




    //浏览记录-------------------------------------------------------------------------
    public function userview()
    {
    	$this->display();
    }

}